package com.baizhi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baizhi.dao.OrderDetailDao;
import com.baizhi.dao.OrderMasterDao;
import com.baizhi.entity.Cart;
import com.baizhi.entity.OrderDetail;
import com.baizhi.entity.OrderMaster;
import com.baizhi.service.OrderMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author yystart
 * @create 2019-10-05 1:01
 */
@Service
public class OrderMasterServiceImpl implements OrderMasterService {

    @Autowired
    private OrderMasterDao masterDao;

    @Autowired
    private OrderDetailDao detailDao;

    @Override
    public Map selectMastersByPage(int page, int limit) {
        int offset = page*limit - limit;
        List<OrderMaster> masters = masterDao.selectMastersByPage(offset, limit);
        int totalCount = masterDao.selectTotalCount();

        Map map = new HashMap();
        map.put("data",masters);
        map.put("count",totalCount);
        map.put("code",0);
        map.put("msg","");

        return map;
    }

    @Override
    @Transactional
    public Map insert(Map dataMap) {
        //取出购物车对象以及订单对象
        JSONObject masterJson = (JSONObject) dataMap.get("master");
        OrderMaster master = masterJson.toJavaObject(OrderMaster.class);
        JSONObject cartJson = (JSONObject) dataMap.get("cart");
        Cart cart = cartJson.toJavaObject(Cart.class);

        //以时间毫秒数为id
        String orderId = new Date().getTime() + "";
        master.setId(orderId);

        //从购物车获得总价
        master.setAmount(cart.getAmount());

        //将订单信息添加到数据库
        int count = 0;
        count += masterDao.insert(master);

        //取出订单详情信息
        Map<Integer, OrderDetail> detailMap = cart.getOrderDetailMap();
        List<OrderDetail> detailList = new ArrayList<>();
        //循环将订单信息赋值并将其放入到list集合中
        for (Integer key : detailMap.keySet()) {
            OrderDetail detail = detailMap.get(key);
            detail.setId(new Date().getTime()+UUID.randomUUID().toString().substring(3));
            detail.setOrderMaster(master);
            detailList.add(detail);
        }

        //将订单详情信息添加到数据库
        count += detailDao.multiInsert(detailList);

        Map map = new HashMap();
        if(count > 0){
            map.put("isInsert",true);
            map.put("msg","添加订单成功！");
        } else {
            map.put("isInsert",false);
            map.put("msg","添加订单失败！");
        }

        return map;
    }

    @Override
    @Transactional
    public Map updateStatus(String id) {
        int count = masterDao.updateStatus(id);
        Map map = new HashMap();
        if(count>0){
            map.put("isUpdate",true);
        } else {
            map.put("isUpdate",false);
        }

        return map;
    }

    @Override
    @Transactional
    public Map delete(String id) {
        int count = masterDao.delete(id);
        Map map = new HashMap();
        if(count>0){
            map.put("isDelete",true);
        } else {
            map.put("isDelete",false);
        }

        return map;
    }

    @Override
    public List<OrderDetail> selectDetailsByMasterId(String orderId) {
        return detailDao.selectDetailsByMasterId(orderId);
    }
}
